/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.backingbean;

import com.aal.entitybean.Products;
import com.aal.sessionbean.product.ProductsFacade;
import com.myapp.Entity.ShoppingCart;
import com.myapp.Entity.ShoppingCartItem;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Windows7
 */
@ManagedBean
@RequestScoped
public class ShoppingCartManagedBean {

    /** Creates a new instance of ShoppingCartManagedBean */
    public ShoppingCartManagedBean() {
    }

    public void add2cart() {

        try {
            FacesContext fct = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) fct.getExternalContext().getSession(true);
            Map map = fct.getExternalContext().getRequestParameterMap();

            String proId = (String) map.get("proID");

            String proName = (String) map.get("proName");

            float price = Float.valueOf((String) map.get("price"));
            String image = (String) map.get("image");

            ShoppingCartItem cartItem = new ShoppingCartItem(proId, proName, image, price);

            if (session.getAttribute("ShoppingCart") == null) {
                ShoppingCart cart = new ShoppingCart();
                cart.add2cart(cartItem);
                session.setAttribute("ShoppingCart", cart);

            } else {
                ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
                cart.add2cart(cartItem);
            }
        } catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }

    }

    public void checkout() throws IOException {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
            ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
            cart.deleteCart();
            session.setAttribute("ShoppingCart", cart);
        } catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

    }

    public ShoppingCartItem[] getShoppingCartItems() {
        try {
            FacesContext fct = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) fct.getExternalContext().getSession(true);

            if (session.getAttribute("ShoppingCart") != null) {
                ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
                return cart.getShoppingCartItems();
            }
        } catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return null;
    }

    public void removeItemProductInCart() {
        try {
            FacesContext fact = FacesContext.getCurrentInstance();
            Map map = fact.getExternalContext().getRequestParameterMap();
            String ProID = (String) map.get("proID");

            HttpSession session = (HttpSession) fact.getExternalContext().getSession(true);
            ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
            cart.removeItemInCart(ProID);
            session.setAttribute("ShoppingCart", cart);
        } catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

    }

    public float getSubTotal() {
        FacesContext fact = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fact.getExternalContext().getSession(true);
        ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
        return cart.getSubTotal();
    }

    public boolean checkexistCart() {
        boolean flag;
        FacesContext fact = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fact.getExternalContext().getSession(true);
        ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
        if (cart != null) {
            if (cart.getShoppingCartItems().length > 0) {
                flag = true;
            } else {
                flag = false;
            }

        } else {
            flag = false;
        }
        return flag;
    }


}
